package pos.utils;

import java.io.File;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;

/**
 * 路径资源库
 * @author plus
 */
public class PathRes {
    //private static HashMap<String, String> PathTable = null;
    private static DocumentBuilderFactory factory;
    private static DocumentBuilder builder;
    private static Document document;
    private static XPathFactory xpfactory;
    private static XPath xpath;
    
    static{
        factory = DocumentBuilderFactory.newInstance();
        try {
            builder = factory.newDocumentBuilder();
            document = builder.parse(new File("res/filepath/path.xml"));
            xpfactory = XPathFactory.newInstance();
            xpath = xpfactory.newXPath();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*public static void init()
    {
        PathTable = new HashMap<String, String>();
        PathTable.put("Config.XML", new File("config/config.xml").getAbsolutePath());
        
        PathTable.put("Image.Loading", new File("res/img/LoadingLogo.jpg").getAbsolutePath());
        
        PathTable.put("Image.MainMenu.Shh", new File("res/img/mainMenu/shhsight.png").getAbsolutePath());
        PathTable.put("Image.MainMenu.Exit", new File("res/img/mainMenu/exit.png").getAbsolutePath());
        PathTable.put("Image.MainMenu.Enter", new File("res/img/mainMenu/enter.png").getAbsolutePath());
        PathTable.put("Image.MainMenu.Drive", new File("res/img/mainMenu/metro.png").getAbsolutePath());
        PathTable.put("Image.MainMenu.Option", new File("res/img/mainMenu/option.png").getAbsolutePath());
        
        PathTable.put("Image.Config.Back", new File("res/img/config/back.png").getAbsolutePath());
        PathTable.put("Image.Config.Cancel", new File("res/img/config/cancel.png").getAbsolutePath());
        PathTable.put("Image.Config.Save", new File("res/img/config/save.png").getAbsolutePath());
        PathTable.put("Image.Config.GFX", new File("res/img/config/gfx.png").getAbsolutePath());
        PathTable.put("Image.Config.SFX", new File("res/img/config/sfx.png").getAbsolutePath());
        PathTable.put("Image.Config.CTRL", new File("res/img/config/ctrl.png").getAbsolutePath());
        
        PathTable.put("Image.MessageBox.Back", new File("res/img/messageBox/back.png").getAbsolutePath());
        PathTable.put("Image.MessageBox.Black", new File("res/img/messageBox/black.png").getAbsolutePath());
        
        PathTable.put("Image.DriveRoom.SpeedDash", new File("res/img/DriveRoom/SpeedDash.png").getAbsolutePath());
        PathTable.put("Image.DriveRoom.AC03.HandleBack", new File("res/img/DriveRoom/handleback_ac03.png").getAbsolutePath());
        PathTable.put("Image.DriveRoom.AC03.Handle", new File("res/img/DriveRoom/handle_ac03.png").getAbsolutePath());
        PathTable.put("Image.DriveRoom.EBButton", new File("res/img/DriveRoom/EBbutton.png").getAbsolutePath());
        PathTable.put("Image.DriveRoom.DoorTips", new File("res/img/DriveRoom/doorTips.png").getAbsolutePath());
        PathTable.put("Image.DriveRoom.Loading", new File("res/img/DriveRoom/Loading.png").getAbsolutePath());

        PathTable.put("Sfx.AC06.DoorClose", new File("res/sfx/AC06/DoorClose.mp3").getAbsolutePath());
        PathTable.put("Sfx.AC06.DoorOpen", new File("res/sfx/AC06/DoorOpen.mp3").getAbsolutePath());
        
        PathTable.put("Model.MainMenu.Static", new File("res/mod/mainMenu/static.obj").getAbsolutePath());
        PathTable.put("Model.MainMenu.Back", new File("res/mod/mainMenu/back.obj").getAbsolutePath());
        
        PathTable.put("Model.Rail.Rail10MDual", new File("res/mod/rail/10mDualRail.obj").getAbsolutePath());
        PathTable.put("Model.Rail.Base50MDaozha", new File("res/mod/rail/50Mdaozha.obj").getAbsolutePath());
        PathTable.put("Model.Rail.Base50MDaozhaR", new File("res/mod/rail/50MdaozhaR.obj").getAbsolutePath());
        PathTable.put("Model.Rail.Base50MDaozhaStation", new File("res/mod/rail/50MdaozhaStation.obj").getAbsolutePath());
        PathTable.put("Model.Rail.Base50MSingleLift", new File("res/mod/rail/50mSingleBridge.obj").getAbsolutePath());
        PathTable.put("Model.Rail.Base50MSingleTunnel", new File("res/mod/rail/50mSingleTunnel.obj").getAbsolutePath());
        PathTable.put("Model.Rail.ATP.Speed", new File("res/mod/rail/atp/speed.obj").getAbsolutePath());
        PathTable.put("Model.Rail.Endder", new File("res/mod/rail/daodang.obj").getAbsolutePath());

        PathTable.put("Model.Segments.S01_01", new File("res/mod/segments/01_01.obj").getAbsolutePath());
        
        PathTable.put("Model.Train.AC01.A", new File("res/mod/train/AC01/A.obj").getAbsolutePath());
        PathTable.put("Model.Train.AC01.B", new File("res/mod/train/AC01/B.obj").getAbsolutePath());
        PathTable.put("Model.Train.AC01.C", new File("res/mod/train/AC01/C.obj").getAbsolutePath());
        PathTable.put("Model.Train.AC01.Cr", new File("res/mod/train/AC01/C.obj").getAbsolutePath());
        PathTable.put("Model.Train.AC01.Br", new File("res/mod/train/AC01/Br.obj").getAbsolutePath());
        PathTable.put("Model.Train.AC01.Ar", new File("res/mod/train/AC01/Ar.obj").getAbsolutePath());
        PathTable.put("Model.Train.AC01.Bogie", new File("res/mod/train/AC01/Bogie.obj").getAbsolutePath());
        PathTable.put("Model.Train.AC01.DoorA", new File("res/mod/train/AC01/DoorA.obj").getAbsolutePath());
        PathTable.put("Model.Train.AC01.DoorB", new File("res/mod/train/AC01/DoorB.obj").getAbsolutePath());
        PathTable.put("Model.Train.AC01.DoorLight", new File("res/mod/train/AC01/DoorLight.obj").getAbsolutePath());

        PathTable.put("Data.Train.AC01", new File("res/data/train/AC01.xml").getAbsolutePath());
        
       // PathTable.put("Surrounding.UM1", new File("res/model/surround/surr.obj").getAbsolutePath());
    }*/

    public static String getPath(String Key)
    {
        String xmlPath = "/path/"+Key.replace('.', '/');
        String fPath;
        try{
            fPath = xpath.evaluate(xmlPath, document);
        }catch(Exception e){
            System.err.println(xmlPath+" Not Found?!");
            e.printStackTrace();
            return null;
        }
        return (new File(fPath).getAbsolutePath());
        /*if (PathTable == null)
        {
            init();
        }
        //
        String res = PathTable.get(Key);
        if (res == null)
        {
            System.err.println("Key"+ Key+" Not Exist!");
        }
        return res;*/
    }

}
